package com.example.windswing;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.material.textfield.TextInputEditText;

import java.nio.charset.StandardCharsets;

public class CtrlActivity extends AppCompatActivity {
    private WindSwingService windSwingService;
    private Handler handler;

    private WindSwingCommand windSwingCommand;

    private Button button;  //reset

    private Button btn_moto1_positive;
    private Button btn_moto2_positive;
    private Button btn_moto3_positive;
    private Button btn_moto4_positive;

    private Button btn_moto1_negative;
    private Button btn_moto2_negative;
    private Button btn_moto3_negative;
    private Button btn_moto4_negative;

    private Button btn_moto1_stop;
    private Button btn_moto2_stop;
    private Button btn_moto3_stop;
    private Button btn_moto4_stop;

    private Button btn_ctrl_liner;
    private Button btn_ctrl_circle;
    private Button btn_ctrl_stop;

    View alert_view;
    TextInputEditText alert_edittext;
    private AlertDialog.Builder ctrl_linerBuilder;
    private AlertDialog.Builder ctrl_circleBuilder;

    private AlertDialog linerDialog;
    private AlertDialog circleDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ctrl);

        windSwingCommand = new WindSwingCommand();
        windSwingCommand.setOnSendCallback(data -> {
            if(windSwingService != null) {
                windSwingService.sendData(data.getBytes(StandardCharsets.UTF_8));
            }
        });

        initAlertDialog();

        button = findViewById(R.id.button); //reset

        btn_moto1_positive = findViewById(R.id.ctrl_moto1_positive);
        btn_moto2_positive = findViewById(R.id.ctrl_moto2_positive);
        btn_moto3_positive = findViewById(R.id.ctrl_moto3_positive);
        btn_moto4_positive = findViewById(R.id.ctrl_moto4_positive);

        btn_moto1_negative = findViewById(R.id.ctrl_moto1_negative);
        btn_moto2_negative = findViewById(R.id.ctrl_moto2_negative);
        btn_moto3_negative = findViewById(R.id.ctrl_moto3_negative);
        btn_moto4_negative = findViewById(R.id.ctrl_moto4_negative);

        btn_moto1_stop = findViewById(R.id.ctrl_moto1_stop);
        btn_moto2_stop = findViewById(R.id.ctrl_moto2_stop);
        btn_moto3_stop = findViewById(R.id.ctrl_moto3_stop);
        btn_moto4_stop = findViewById(R.id.ctrl_moto4_stop);

        button.setOnClickListener(view -> {
            windSwingCommand.reset();
        });
        btn_moto1_positive.setOnClickListener(view -> {
            windSwingCommand.singleMotoCtrl(1, WindSwingCommand.MOTO_POSITIVE);
        });
        btn_moto2_positive.setOnClickListener(view -> {
            windSwingCommand.singleMotoCtrl(2, WindSwingCommand.MOTO_POSITIVE);
        });
        btn_moto3_positive.setOnClickListener(view -> {
            windSwingCommand.singleMotoCtrl(3, WindSwingCommand.MOTO_POSITIVE);
        });
        btn_moto4_positive.setOnClickListener(view -> {
            windSwingCommand.singleMotoCtrl(4, WindSwingCommand.MOTO_POSITIVE);
        });

        btn_moto1_negative.setOnClickListener(view -> {
            windSwingCommand.singleMotoCtrl(1, WindSwingCommand.MOTO_NEGATIVE);
        });
        btn_moto2_negative.setOnClickListener(view -> {
            windSwingCommand.singleMotoCtrl(2, WindSwingCommand.MOTO_NEGATIVE);
        });
        btn_moto3_negative.setOnClickListener(view -> {
            windSwingCommand.singleMotoCtrl(3, WindSwingCommand.MOTO_NEGATIVE);
        });
        btn_moto4_negative.setOnClickListener(view -> {
            windSwingCommand.singleMotoCtrl(4, WindSwingCommand.MOTO_NEGATIVE);
        });

        btn_moto1_stop.setOnClickListener(view -> {
            windSwingCommand.singleMotoCtrl(1, WindSwingCommand.MOTO_STOP);
        });
        btn_moto2_stop.setOnClickListener(view -> {
            windSwingCommand.singleMotoCtrl(2, WindSwingCommand.MOTO_STOP);
        });
        btn_moto3_stop.setOnClickListener(view -> {
            windSwingCommand.singleMotoCtrl(3, WindSwingCommand.MOTO_STOP);
        });
        btn_moto4_stop.setOnClickListener(view -> {
            windSwingCommand.singleMotoCtrl(4, WindSwingCommand.MOTO_STOP);
        });

        btn_ctrl_liner = findViewById(R.id.ctrl_liner);
        btn_ctrl_liner.setOnClickListener(view -> {
            if(linerDialog != null){
                linerDialog.dismiss();
                linerDialog = null;
            }
            alert_view = View.inflate(this, R.layout.input_dialog, null);
            alert_edittext = alert_view.findViewById(R.id.dialog_inputedittext);
            linerDialog = ctrl_linerBuilder.setView(alert_view).show();
        });

        btn_ctrl_circle = findViewById(R.id.ctrl_circle);
        btn_ctrl_circle.setOnClickListener(view -> {
            if(circleDialog != null){
                circleDialog.dismiss();
                linerDialog = null;
            }
            alert_view = View.inflate(this, R.layout.input_dialog, null);
            alert_edittext = alert_view.findViewById(R.id.dialog_inputedittext);
            circleDialog = ctrl_circleBuilder.setView(alert_view).show();
        });

        btn_ctrl_stop = findViewById(R.id.ctrl_stop);
        btn_ctrl_stop.setOnClickListener(view -> {
            windSwingCommand.stop();
        });
    }

    void initAlertDialog(){
        alert_view = View.inflate(this, R.layout.input_dialog, null);
        alert_edittext = alert_view.findViewById(R.id.dialog_inputedittext);

        ctrl_linerBuilder = new AlertDialog.Builder(this);
        ctrl_linerBuilder.setTitle("请输入直线摆动路径方位角")
                .setView(alert_view)
                .setPositiveButton("确认", (dialogInterface, i) -> {
                    if(alert_edittext.getText() != null && !alert_edittext.getText().toString().equals("")){
                        int angle = Integer.parseInt(alert_edittext.getText().toString());
                        windSwingCommand.swing(angle);
                    }else {
                        Toast.makeText(CtrlActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
                    }
                    linerDialog.dismiss();
                    linerDialog = null;
                });

        ctrl_circleBuilder = new AlertDialog.Builder(this);
        ctrl_circleBuilder.setTitle("请输入圆的半径")
                .setView(alert_view)
                .setPositiveButton("确认", (dialogInterface, i) -> {
                    if(alert_edittext.getText() != null && !alert_edittext.getText().toString().equals("")){
                        int radium = Integer.parseInt(alert_edittext.getText().toString());
                        windSwingCommand.circle(radium);
                    }else {
                        Toast.makeText(CtrlActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
                    }
                    circleDialog.dismiss();
                    circleDialog = null;
                });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent(this, WindSwingService.class);
        bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);
    }

    private final ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            WindSwingService.ServiceBinder binder = (WindSwingService.ServiceBinder) iBinder;
            windSwingService = binder.getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            windSwingService = null;
        }
    };
}